package com.englisheveryday.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.englisheveryday.R;
import com.englisheveryday.constants.Constants;
import com.englisheveryday.media.RichMediaPlayer;
import com.englisheveryday.media.SimpleMediaPlayer;
import com.englisheveryday.media.SimpleMediaPlayerService;
import com.englisheveryday.service.PlaylistService;
import com.englisheveryday.utils.ActivityManager;
import com.englisheveryday.utils.L;
import com.englisheveryday.vo.PlaylistVO;

public class PlayListActivity extends Activity {
	
	private LinearLayout playlistLinearLayout;
	private Handler handler;
	private PlaylistService playlistService;
	private List<PlaylistVO> playlist;
	private RichMediaPlayer player;
    private ThreadPoolExecutor executor;
    private SimpleMediaPlayer smp;
    private SimpleMediaPlayerReceiver receiver;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlist_activity);
		ActivityManager.add(this);
		playlistService = new PlaylistService(this);
		executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(3);
		setViews();
		setListeners();
		playlist = new ArrayList<PlaylistVO>();
		handler = new PlaylistRefreshHandler();
		L.d("play.....");
		receiver = new SimpleMediaPlayerReceiver(handler);
		IntentFilter intentFilter = new IntentFilter("com.englisheveryday.mediaplayer.info");
		registerReceiver(receiver, intentFilter);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}


	@Override
	protected void onResume() {
		super.onResume();
		executor.submit(new RefreshThread());
	}
	
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
		smp.onDestroy();
	}

	public void setViews(){
		playlistLinearLayout = (LinearLayout)findViewById(R.id.playlistlinearlayout);
		smp = (SimpleMediaPlayer)findViewById(R.id.simpleMediaPlayer);
		smp.setApplicationContext(getApplicationContext());
		smp.init();
	}
	
	public void setListeners(){
	
	}
	

	
	class PlaylistRefreshHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case Constants.HANDLER_PLAYLIST_LOADED : {
				refreshPlaylist();
				//获得当前播放
				
			}
			break;
			case Constants.HANDLER_PLAYLIST_CHECKED : {
				Bundle bundle = msg.getData();
				playlistchecked(bundle.getString("id"));
			}
			break;
			default : {
				
			}
			}
		}
		
	}
	
	/**
	 * 刷新播放列表
	 */
	void refreshPlaylist(){
	playlistLinearLayout.removeAllViews();
	for(int i=0;i<playlist.size();i++){
		View view = LayoutInflater.from(PlayListActivity.this).inflate(R.layout.playlist_item,null);
		final PlaylistVO vo = playlist.get(i);
		TextView titleView = (TextView)view.findViewById(R.id.title);
		titleView.setText(vo.getTitle());
		view.setTag(vo.getId());
		playlistLinearLayout.addView(view,2*i);
		LinearLayout divider = new LinearLayout(this);
		divider.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,1));
		divider.setBackgroundResource(R.color.light_green);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				smp.setPlayId(vo.getId());
				smp.play();
				L.d(vo.getId());
			}
		});
		playlistLinearLayout.addView(divider,2*i+1);
	}
	}
	
	/**
	 * 播放列表选择
	 */
	void playlistchecked(String id){
	 LinearLayout linearLayout  = (LinearLayout)playlistLinearLayout.findViewWithTag(id);
	 TextView title = (TextView)linearLayout.findViewById(R.id.title);
	 title.setTextColor(getResources().getColor(R.color.playlistcolor));
	}
	
	//载入播放列表
	class RefreshThread implements Runnable {
		@Override
		public void run() {
			playlist.clear();
			playlist.addAll(playlistService.findAll());
			handler.sendEmptyMessage(Constants.HANDLER_PLAYLIST_LOADED);
		}
		
	}
}